﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Data;
using System;

public class BattleHandler : IHander {

    public void Register(Dictionary<Protocal, Action<ByteBuffer>> handlers)
    {
        handlers.Add(Protocal.PlayerMove_CMD, OnPlayerMove);
        handlers.Add(Protocal.PlayerFire_CMD, OnPlayerFire);
        handlers.Add(Protocal.PlayerDamage_CMD, OnPlayerDamage);
        handlers.Add(Protocal.PlayerDead_CMD, OnPlayerDead);
    }

    private void OnPlayerDead(ByteBuffer buffer)
    {
        ReqHit hit = ProtoHelper.Deserialize<ReqHit>(buffer.ReadBytes());
        ActorManager.Instance.GetTank(hit.hurtname).TakeHurt(hit.hp);
        ActorManager.Instance.GetTank(hit.hitname).TakeReward();
    }

    private void OnPlayerDamage(ByteBuffer buffer)
    {
        ReqHit hit = ProtoHelper.Deserialize<ReqHit>(buffer.ReadBytes());
        ActorManager.Instance.GetTank(hit.hurtname).TakeHurt(hit.hp);
        ActorManager.Instance.GetTank(hit.hitname).TakeReward();
    }

    private void OnPlayerFire(ByteBuffer buffer)
    {
        ReqPosition resp = ProtoHelper.Deserialize<ReqPosition>(buffer.ReadBytes());
        Tank tank = ActorManager.Instance.GetTank(resp.username);
        tank.Fire(resp);
    }

    private void OnPlayerMove(ByteBuffer buffer)
    {
        ReqPosition resp = ProtoHelper.Deserialize<ReqPosition>(buffer.ReadBytes());
        Tank tank = ActorManager.Instance.GetTank(resp.username);
        tank.Move(resp);
    }
}
